<?php
	function processLogin($echo = TRUE){
		if(!$_SESSION["user_id"] && !$_SESSION["username"]){
			createSession(9, "Ringare", 1);
			if($echo)
				echo "Logged as <strong>".$_SESSION["username"]."</strong>";
		}else{
			if($echo)
				echo "Logged as <strong>".$_SESSION["username"]."</strong>";
		}
		/*
		if($_SESSION["user_id"] && $_SESSION["username"]){
			echo "Logged as <strong>".$_SESSION["username"]."</strong>";
		}else{
			if(!isLogged()){
				if(!loginByCookie()){
					echo "Not Logged";
				}else{
					echo "Logged as <strong>".$_SESSION["username"]."</strong>";
				}
			}else{				
				echo "Logged as <strong>".$_SESSION["username"]."</strong>";
			}
		}*/
	}


	function isLogged(){
		$session_id = $_COOKIE["PHPSESSID"];
		if($session_id){
			$query = new mysqlQuery("forum", "", "smf_sessions", "*", "`session_id`='".$session_id."'", "", "");
			$query->execute();
			if($query->getCount() == 1){
				$row = $query->next();
				$data = explode(";", $row["data"]);
				$m = explode("\"", $data[6]);
				$user_id = $m[1];
				if($user_id == 0){
					return FALSE;
				}else{
					$query = new mysqlQuery("forum", "", "smf_members", "`member_name`, `level`, `email_address`", "`id_member`='".$user_id."'", "", "");
					$query->execute();
					if($query->getCount() == 1){
						$row = $query->next();
						createSession($user_id, $row["member_name"], $row["level"], $row["email_address"]);
						return TRUE;
					}else{
						return FALSE;
					}
				}				
			}else{				
				return FALSE;
			}
		}else{
			return FALSE;		
		}
	}
	
	function loginByCookie(){		
		if($_COOKIE["SMFCookie508"]){
			$data = explode(";", $_COOKIE["SMFCookie508"]);
			$m = explode("\\\"", $data[1]);
			$user_id = $m[1];
			$m = explode("\\\"", $data[3]);
			$sha1Premade = $m[1];
			return isValidSha1($user_id, $sha1Premade);
		}else{
			return FALSE;	
		}
	}
	
	function isValidSha1($user_id, $sha1Premade){
		$query = new mysqlQuery("forum", "", "smf_members", "`member_name`, `email_address`, `id_group`, `passwd`, `password_salt`", "`id_member`='".$user_id."'", "", "");
		$query->execute();
		if($query->getCount() == 1){
			$row = $query->next();
			$sha1 = sha1($row["passwd"].$row["password_salt"]);
			if($sha1 == $sha1Premade){
				createSession($user_id, $row["member_name"], $row["id_group"], $row["email_address"]);
				return TRUE;
			}else{
				return FALSE;
			}			
		}else{
			return FALSE;
		}
	}
	
	function createSession($user_id, $username, $level, $email = NULL){		
		$_SESSION["user_id"] = $user_id;
		$_SESSION["username"] = $username;
		$_SESSION["level"] = $level;
		/* REGISTRAR JUGADOR EN DB */
		$query = new mysqlQuery("tournament", "", "usuarios", "*", "`id`='".$user_id."'", "", "");		
		$query->execute();
		if($query->getCount() == 0){
			/* Continent */
			$location = getLocation($_SERVER["REMOTE_ADDR"]);
			if($location == -1){
				$values = array($user_id, $username, $email, $level, "", 1, 0, "Unknown", "XX", "Unknown", 0, 0, 0, 0, 0, 0, 0);
				$insert = new mysqlInsert("tournament", "", "usuarios", $values);
				$insert->execute();
				$_SESSION["continent"] = "Unknown";
				$_SESSION["isNew"] = 1;
			}else{
				$values = array($user_id, $username, $email, $level, "", 1, $location["ip"], $location["contryName"], $location["countryCode"], $location["continent"], 0, 0, 0, 0, 0, 0, 0);
				$insert = new mysqlInsert("tournament", "", "usuarios", $values);
				$insert->execute();
				$_SESSION["continent"] = $location["continent"];
				$_SESSION["isNew"] = 1;
			}
		}else{
			$row = $query->next();
			$_SESSION["player"] = $row["jugador"];
			$_SESSION["isNew"] = $row["isNew"];
			$_SESSION["continent"] = $row["continent"];
			if(strlen($row["email_validation_code"]) == 0){
				$_SESSION["isValidMail"] = 1;
			}else{
				$_SESSION["isValidMail"] = 0;
			}			
		}
	}
	
	function sendValidationEmail(){
		$query = new mysqlQuery("tournament", "", "usuarios", "*", "`id`='".$_SESSION["user_id"]."'", "", "");
		$query->execute();
		if($query->getCount() == 1){
			$row = $query->next();
			$validationCode = md5($_SESSION["username"].time());
			$body = '<p>Welcome to <strong>PES6J Tournament</strong>.</p>
							<p>To validate your account click on the following link:</p>
							<p><a href="http://www.pes6j.net/tournament/index.php?s=validateAccount&u='.$_SESSION["username"].'&code='.$validationCode.'">Validation Link</a></p>';
			$destination = $row["email"];
			$priority = 1;
			$subject = "PES6J Tournament Account Validation";
			
			$fields = array("email_validation_code");
			$values = array($validationCode);
			$update = new mysqlUpdate("tournament", "", "usuarios", $fields, $values, "`id`='".$_SESSION["user_id"]);
			$update->execute();
			
			$mailQueue = new MailQueue();
			$mailQueue->createMail($body, $subject, $destination, $priority);
		}else{
			return FALSE;
		}
	}

	function isAdmin(){
		if(	$_SESSION["level"] == 1 || 
			$_SESSION["level"] == 12)
		{
			return TRUE;
		}else{
			return FALSE;
		}
	}
	
	function checkActionTime(){		
		if(!$_SESSION["action_time"]){
			return TRUE;
		}else{
			if((time() - $_SESSION["action_time"]) < MIN_ACTION_TIME){
				return FALSE;				
			}else{
				return TRUE;
			}
		}
	}
	
	function refreshActionTime(){
		$_SESSION["action_time"] = time();
	}
	
	function getCurrentActionTime(){
		return ceil((time() - $_SESSION["action_time"]) / 60); 
	}
?>